package jp.co.sapporobeer.FmcDialer.dbsync.data;

import java.io.IOException;
import java.util.ArrayList;

import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.JsonToken;

import com.sun.jersey.core.util.Base64;

//import android.database.sqlite.SQLiteStatement;
import info.guardianproject.database.sqlcipher.SQLiteStatement;

public class MyProfileFactory implements ListFactory {
	private String sqlKeyIns = "SQL_INSERT_MYPROFILE";
	private String sqlKeyDel = "SQL_DELETE_MYPROFILE";
	private ArrayList<ListRecord> list = new ArrayList<ListRecord>(); 
	
	public ListRecord getRecords(JsonParser jp) throws JsonParseException,IOException{
        String id = "";
        String profileCategory = "";
        String profileName = "";
        String profileValueT = "";
        String profileValueI = "";
        String profileValueR = "";
        String profileValueB = "";
        while (jp.nextToken() != JsonToken.END_OBJECT) {
            String name = jp.getCurrentName();
            jp.nextToken();
            if ("id".equals(name)) id = jp.getText();
            else if ("profileCategory".equals(name)) profileCategory = jp.getText();
            else if ("profileName".equals(name)) profileName = jp.getText();
            else if ("profileValueT".equals(name)) profileValueT = jp.getText();
            else if ("profileValueI".equals(name)) profileValueI = jp.getText();
            else if ("profileValueR".equals(name)) profileValueR = jp.getText();
            else if ("profileValueB".equals(name)) profileValueB = jp.getText();
            else jp.skipChildren();
        }
        ListRecord rec = new MyProfileRecord(id,profileCategory,profileName,profileValueT,profileValueI,profileValueR,profileValueB);
        return rec;
	}
	
	public ListRecord getSingleRecord(JsonParser jp) throws JsonParseException,IOException{
        String id = "";
        String profileCategory = "";
        String profileName = "";
        String profileValueT = "";
        String profileValueI = "";
        String profileValueR = "";
        String profileValueB = "";
        while (jp.getCurrentToken() != JsonToken.END_OBJECT) {
            String name = jp.getCurrentName();
            jp.nextToken();
            if ("id".equals(name)) id = jp.getText();
            else if ("profileCategory".equals(name)) profileCategory = jp.getText();
            else if ("profileName".equals(name)) profileName = jp.getText();
            else if ("profileValueT".equals(name)) profileValueT = jp.getText();
            else if ("profileValueI".equals(name)) profileValueI = jp.getText();
            else if ("profileValueR".equals(name)) profileValueR = jp.getText();
            else if ("profileValueB".equals(name)) profileValueB = jp.getText();
            else jp.skipChildren();
            jp.nextToken();
        }
        ListRecord rec = new MyProfileRecord(id,profileCategory,profileName,profileValueT,profileValueI,profileValueR,profileValueB);
		return rec;
	}
	
	public void add(ListRecord rec){
		list.add(rec);
	}
	
	public ArrayList<ListRecord> getList(){
		return list;
	}
	
	public String getSqlKeyIns(){
		return sqlKeyIns;
	}

	public String getSqlKeyDel(){
		return sqlKeyDel;
	}

	public void bindRecord(SQLiteStatement stmt,ListRecord rec){
    	stmt.bindLong(1,Integer.parseInt(((MyProfileRecord)rec).getId()));
    	stmt.bindString(2, ((MyProfileRecord)rec).getProfileCategory());
    	stmt.bindString(3, ((MyProfileRecord)rec).getProfileName());
    	stmt.bindString(4, ((MyProfileRecord)rec).getProfileValueT());
    	stmt.bindLong(5, Integer.parseInt(((MyProfileRecord)rec).getProfileValueI()));
    	stmt.bindDouble(6, Double.parseDouble(((MyProfileRecord)rec).getProfileValueR()));
    	stmt.bindBlob(7, Base64.encode(((MyProfileRecord)rec).getProfileValueB()));		
	}
}
